package com.woniu.shops.controller;

import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.Type;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.AjaxResult;
import com.woniu.common.vo.Shopsss;
import com.woniu.common.vo.Typess;
import com.woniu.shops.service.TypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Types")
@CrossOrigin
public class TypeController {
    @Resource
    private TypeService typeService;

    @RequestMapping("findAll")
        public AjaxResult findAll(HttpServletRequest request, @Param("pageNum")Integer pageNum, @Param("pageSize")Integer pageSize) {
        Shops shop = TokenUtil.getObj(request,Shops.class);
        List<Type> typeList = typeService.getTypeList(shop);
        ArrayList<Type> types = new ArrayList<>();
        System.out.println(pageNum+","+pageSize);
        Integer num = (pageNum-1)*pageSize;
        Integer is = typeList.size() - pageSize - num;
        if(is < 0){
            for (int i = num; i < typeList.size(); i++) {
                types.add(typeList.get(i));
            }
        }else {
            for (int i = num; i < num+pageSize; i++) {
                types.add(typeList.get(i));
            }
        }
        Typess typess = new Typess(types,typeList.size());
        return AjaxResult.success("类型准备完毕:",typess);
    }
    @RequestMapping("findAlls")
        public AjaxResult findAlls(HttpServletRequest request) {
        Shops shop = TokenUtil.getObj(request,Shops.class);
        List<Type> typeList = typeService.getTypeList(shop);
        return AjaxResult.success("类型准备完毕:",typeList);
    }
    @RequestMapping("deleteType")
    public AjaxResult deleteType(@RequestBody Type types) {
        System.out.println(types);
        typeService.deleteType(types);
        return AjaxResult.success("类型删除完毕:");
    }
    @RequestMapping("updateType")
    public AjaxResult updateType(@RequestBody Type types) {
        System.out.println(types);
        typeService.updateType(types);
        return AjaxResult.success("类型修改完毕:");
    }
    @RequestMapping("addTypes")
    public AjaxResult addTypes(@RequestBody Type types) {
        System.out.println(types);
        typeService.addTypes(types);
        return AjaxResult.success("类型添加完毕:");
    }

}
